package org.lsms.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.lsms.bean.User;
import org.lsms.dao.impl.DaoImpl;

public class signup extends HttpServlet {

	/**
	 * servlet used to process user sign up.
	 * @author ZhanXueLiang
	 * 2010.8.9
	 */
	public void destroy() {
		super.destroy(); 
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
			this.doPost(request, response);
	}


	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
			User user = new User();
			user.setLoginname(request.getParameter("loginname"));
			user.setAddress(request.getParameter("address"));
			user.setAffiliation(request.getParameter("affiliation"));
			user.setCountry(request.getParameter("country"));
			user.setEmail(request.getParameter("email"));
			user.setFax(request.getParameter("fax"));
			user.setFirstname(request.getParameter("firstname"));
			user.setMiddlename(request.getParameter("middlename"));
			user.setLastname(request.getParameter("lastname"));
			user.setPassword(request.getParameter("password"));
			user.setTel(request.getParameter("tel"));
			user.setTitle(request.getParameter("title"));
			user.setPermission(3);
			user.setLogintimes(1);
			
			HttpSession session = request.getSession();
			DaoImpl impl = new DaoImpl();
			
			if(impl.check_login_name(request.getParameter("loginname"))){
				if(impl.signup(user)){
					session.setAttribute("username", user.getFirstname());
					session.setAttribute("userid", impl.get_user_id(request.getParameter("loginname")));
					session.setAttribute("logintimes", 1);
					response.sendRedirect("author/author_welcome.jsp");
				}
				else{
					response.sendRedirect("error/signup_fail.jsp");
				}
			}else{
				String info = "The login name "+request.getParameter("loginname")+" has " +
						"been used!Please choose anothor one.";
				response.sendRedirect("show_info.jsp?info="+info);
			}
			
			
	}


	public void init() throws ServletException {
	
	}

}
